pi_orca 0.4.4

A* Path Finding Algorithm
<!DOCTYPE html>
<html>

<head>
  <title>我的网页</title>
</head>

<body>
  <script type="module">

    const PI = 3.141592653589793238;

    import init from './pkg/pi_orca.js';
    import { RVOSimulator } from './pkg/pi_orca.js';

    const temp = 100;
    const num = 50;
    const remove_id = num / 2;
    const maxSpeed = 5;
    let lastpotion = [];

    let is_remove = false;

    class Vector2 {
      constructor(obj) {
        this.x = obj.x;
        this.y = obj.y;
      }
      static new(x, y) {
        return new Vector2({ x, y });
      }

      sub(other) {
        return Vector2.new(this.x - other.x, this.y - other.y);
      }

      mul_number(other) {
        return Vector2.new(this.x * other, this.y * other);
      }

      static abs_sq(other) {
        return other.x * other.x + other.y * other.y;
      }

      static normalize(other) {
        let length = Math.sqrt(Vector2.abs_sq(other));
        return Vector2.new(other.x / length, other.y / length);
      }
    }
    // 0
    function setup_scenario(sim, goals, divs, agents) {
      sim.set_time_step(0.25);

      sim.set_agent_defaults(15, 10, 10.0, 10.0, 2.5, maxSpeed, 0.0, 0.0);
      // for (let i = 0; i < num; i++) {
      let x = 50;
      let y = 50;


      divs[0] = document.createElement("div");
      divs[0].style.left = Math.floor(x + temp) + "px";
      divs[0].style.top = Math.floor(y + temp) + "px";
      divs[0].style.width = "4px";
      divs[0].style.height = "4px";
      divs[0].style.borderRadius = "50%";
      divs[0].style.backgroundColor = "rgb(1, 0, 0)";
      // console.log(Math.floor(1 / num * 256));
      divs[0].style.position = "absolute";
      divs[0].id = "div" + 0;
      document.body.appendChild(divs[0]);

      agents[0] = sim.add_agent(100, 0, maxSpeed);
      // console.log("-sim.getAgentPosition(i): ", sim.get_agent_position(i).neg());
      sim.set_agent_goal(agents[0], 0, 0);
      // sim.set_agent_velocity(agents[0], -5, 0);

      divs[1] = document.createElement("div");
      divs[1].style.left = Math.floor(x + temp) + "px";
      divs[1].style.top = Math.floor(y + temp) + "px";
      divs[1].style.width = "4px";
      divs[1].style.height = "4px";
      divs[1].style.borderRadius = "50%";
      divs[1].style.backgroundColor = "rgb(1, 0, 0)";
      // console.log(Math.floor(1 / num * 256));
      divs[1].style.position = "absolute";
      divs[1].id = "div" + 1;
      document.body.appendChild(divs[1]);

      agents[1] = sim.add_agent(120, 0, maxSpeed);
      // console.log("-sim.getAgentPosition(i): ", sim.get_agent_position(i).neg());
      sim.set_agent_goal(agents[1], -20, 0);

      // divs[2] = document.createElement("div");
      // divs[2].style.left = Math.floor(x + temp) + "px";
      // divs[2].style.top = Math.floor(y + temp) + "px";
      // divs[2].style.width = "4px";
      // divs[2].style.height = "4px";
      // divs[2].style.borderRadius = "50%";
      // divs[2].style.backgroundColor = "rgb(1, 0, 0)";
      // // console.log(Math.floor(1 / num * 256));
      // divs[2].style.position = "absolute";
      // divs[2].id = "div" + 2;
      // document.body.appendChild(divs[2]);

      // agents[2] = sim.add_agent(-100, 0, maxSpeed);
      // // console.log("-sim.getAgentPosition(i): ", sim.get_agent_position(i).neg());
      // sim.set_agent_goal(agents[2], 100, 0);

      // divs[3] = document.createElement("div");
      // divs[3].style.left = Math.floor(x + temp) + "px";
      // divs[3].style.top = Math.floor(y + temp) + "px";
      // divs[3].style.width = "4px";
      // divs[3].style.height = "4px";
      // divs[3].style.borderRadius = "50%";
      // divs[3].style.backgroundColor = "rgb(1, 0, 0)";
      // // console.log(Math.floor(1 / num * 256));
      // divs[3].style.position = "absolute";
      // divs[3].id = "div" + 3;
      // document.body.appendChild(divs[3]);

      // agents[3] = sim.add_agent(100, 0, maxSpeed);
      // // console.log("-sim.getAgentPosition(i): ", sim.get_agent_position(i).neg());
      // sim.set_agent_goal(agents[3], -100, 0);
      // }
    }
    let last_position = [];
    function update_visualization(sim, divs, agents) {
      console.log("", sim.get_global_time());
      let result = true;
      for (let i = 0; i < agents.length; i++) {
        if (i == remove_id && is_remove) {
          continue;
        }
        let position = sim.get_agent_position(agents[i]);
        // lastpotion[i] = position;


        let velocity = sim.get_agent_velocity(agents[i]);
        let pref_velocity = sim.get_agent_pref_velocity(agents[i]);

        let x = Math.floor(position.x) + temp;
        let y = Math.floor(position.y) + temp;
        // console.log("Agent2: " + i + "; x: " + x + "; y: " + y);

        divs[i].style.left = x + "px";
        divs[i].style.top = y + "px";
        // console.log("divs[i]: ", divs[i]);

        // console.log("Agent: " + i + "; x: " + position.x + "; y: " + position.y);
        let goal = sim.get_agent_goal(agents[i]);
        console.log("当前位置: ", position);
        console.log("目标位置: ", goal);
        console.log("期望速度:", pref_velocity)
        if (last_position.length != agents.length) {
          last_position[i] = position;
        } else {
          let v_x = position.x - last_position[i].x;
          let v_y = position.y - last_position[i].y;
          console.log("当前速度:", { x: v_x, y: v_y })
          // if (v_x == 0 && v_y == 0) {
          //   debugger
          // }
          last_position[i] = position;
        }
        if (goal) {
          let max_speed = sim.get_agent_max_speed(agents[i]);
          // console.log("max_speed: ", max_speed);
          let custom_speed = sim.get_agent_custom_speed(agents[i]);
          // console.log("custom_speed: ", custom_speed);
          let speed = max_speed > custom_speed ? custom_speed : max_speed;
          // console.log("speed: ", speed);
          let position = sim.get_agent_position(agents[i]);

          // console.log("position: ", position);
          let dist_pos = Vector2.new(goal.x, goal.y).sub(Vector2.new(position.x, position.y));
          // console.log("dist_pos: ", dist_pos);
          let dist_sq = Vector2.abs_sq(dist_pos);
          let velocity = Vector2.normalize(dist_pos).mul_number(speed * sim.get_time_step());
          // console.log("velocity: ", velocity);
          // console.log("Vector2.abs_sq(velocity): ", Vector2.abs_sq(velocity));
          if (dist_sq > 0.001) {
            // pref_velocity = velocity;
            // 还未到达目标点
            result = false;
          } else {
            // sim.set_agent_goal(agents[i], null, null);
          }
        }
        console.log("");
      }

      if (result) {
        console.log("到达目标点");
      }
      return result;
    }

    // function reached_goal(sim, goals) {
    //   /* Check if all agents have reached their goals. */
    //   for (let i = 0; i < num; i++) {
    //     if (i == remove_id && is_remove) {
    //       continue;
    //     }
    //     let position = sim.get_agent_position(i);
    //     if (Vector2.abs_sq(goals[i].sub(sim.get_agent_position(agents[i]))) > 2) {

    //       return false;
    //     }
    //   }

    //   return true;
    // }

    init().then(module => {
      // 在这里调用 Rust 函数
      // console.log(module)

      let sim = RVOSimulator.default();
      let goals = [];
      let divs = [];
      let agents = [];


      setup_scenario(sim, goals, divs, agents);

      let begin = performance.now();
      let r = 0;
      let id = setInterval(() => {
        let begin = performance.now();
        if (update_visualization(sim, divs, agents)) {
          clearInterval(id);
        }
        let res = sim.do_step();
        // console.log("======= time: ", performance.now() - begin, " res: " + res);
      }, 16)
    });


  </script>
</body>

</html>